package com.lifang.imsoa.timetask;

import java.util.Calendar;
import java.util.Date;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.lifang.imsoa.base.service.QuartzTaskBase;
import com.lifang.imsoa.im.service.EasemobChatMessage;
import com.lifang.imsoa.util.DateUtils;

@Slf4j
@Component
public class DragChatMsgTask {
	@Autowired
	private EasemobChatMessage easemobChatMessage;
	
	/**
	 * 功能描述:下载前一天的聊天记录,每天凌晨0点5分执行
	 */
	@Scheduled(cron = "0 5 0 * * ?") 
	public void DragChatMsgDaily(){
		if(isRunServer()){
			Date now = new Date();
			String nowStr = DateUtils.formatDateByPattern(now, DateUtils.defalutDateFormat);
			int sumCount = 0;
			log.info("下载聊天记录开始,操作时间={}",nowStr);
			Date start = DateUtils.addDays(now, -1);//前一天00:00:00
			Date end = DateUtils.ridTimeFromDay(now);//今天00:00:00
			sumCount = easemobChatMessage.getChatMsg(start, end ,true);
			log.info("下载聊天记录结束,操作时间={},下载总条数={}",nowStr,sumCount);
		}
	}
	
//	@Scheduled(cron = "0 0 20 19 11 ?")
	public void DragAllChatMsg(){
		if(isRunServer()){
			Calendar cal = Calendar.getInstance();
			if(cal.get(Calendar.YEAR)==2015){
				Date now = new Date();
				String nowStr = DateUtils.formatDateByPattern(now, DateUtils.defalutDateFormat);
				int sumCount = 0;
				log.info("下载聊天记录开始,操作时间={}",nowStr);
				Date start = DateUtils.getDateBySetNo(2015, 10, 24, 0, 0, 0);
				Date end = DateUtils.getDateBySetNo(2015, 11, 19, 0, 0, 0);
				sumCount = easemobChatMessage.getChatMsg(start, end ,true);
				log.info("下载聊天记录结束,操作时间={},下载总条数={}",nowStr,sumCount);
			}
		}
	}
	
	private boolean isRunServer() {
		if (QuartzTaskBase.isRun()) {
			return true;
		} else {
			log.info("这台机器不跑定时任务！");
		}
		return false;
	}
}
